<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
    >
      <template #sex="{ row }">{{ row.sex == "1" ? "男" : "女" }} </template>
      <template #actionButton="{ row }">
        <el-button type="text" icon="Edit" @click="handleEdit(row)"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :slots="['fileSlot']"
      :title="dialogType == 'add' ? '新增人员档案' : '修改人员档案'"
    >
      <template #fileSlot>
        <el-form-item label="照片">
          <FileUpload
            :limit="1"
            :fileSize="30"
            :fileType="['doc', 'docx', 'pdf']"
            v-model="files"
          ></FileUpload>
        </el-form-item>
      </template>
    </DialogVform>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/aqsc/xgfgl/ryda.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
const { proxy } = getCurrentInstance();

const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});

const files = ref([]);
watch(
  () => files,
  (newData) => {
    if (newData.value.length === 0) {
      form.value.imgFileId = "";
      form.value.imgFileId = "";
      form.value.imgFileUrl = "";
    } else {
      form.value.imgFileName = newData.value[0].name;
      form.value.imgFileId = newData.value[0].id;
      form.value.imgFileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);
const plugAdd = () => {
  files.value = [];
};
const plugEdit = (row) => {
  if (row.imgFileId) {
    files.value = [
      {
        name: row.imgFileName,
        id: row.imgFileId,
        url: row.imgFileUrl,
      },
    ];
  } else {
    files.value = [];
  }
};
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    [
      "name",
      "sex",
      "company",
      "card",
      "workType",
      "inCompanyTime",
      "personType",
    ].includes(i.prop)
  ),
});
const showSearch = ref(true);

const dialogVform = ref();

const table = ref();

const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  dialogType,
  form,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  plugEdit,
  plugAdd,
});
</script>

<style lang="scss" scoped></style>
